﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model  IEnumerable<Domain.Dto.SightPointCommDto>
@{
    var targetId = ViewBag.id == null ? 0 : ViewBag.id;
    var sightName = ViewBag.sightname == null ? "" : ViewBag.sightname;
    ViewBag.subtitle = "景点评论管理" + (sightName == "" ? "" : "--" + sightName);
    ViewBag.title = "景点评论管理" + (sightName == "" ? "" : "--" + sightName);
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ Html.Telerik().Window()
        .Name("ReplyWindow")
        .Title("回复内容")
        .Content(@<text>
    @Html.Partial("_SightCommReply")
    </text>)
        .Width(650)
        .Height(250)
        .Resizable(res => res.MaxHeight(300).MinHeight(250).MinWidth(650).MaxWidth(700))
        .Buttons(b => b.Maximize().Close())
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
<input id = "targetId" name = "targetId" type = "hidden" value = "@(targetId)"/>
@{Html.Telerik().Grid(Model)
    .Name("SightCommGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "searchsightcomment"))
        {
        <label class="customer-label" for="Customers-input">
            评论内容:
        </label>
        <input type="text" id="searchSightComm" value="评论内容" data-autocomplete="/sight/searchsightcomment" />
        <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' />
        }
        if (Html.HasUiPermission(this.ViewContext, "muldeletesightcomm"))
        { 
        <a href='javascript:;' class='t-button' onclick='mulDelete()'>删除选择项</a>
        }
    }
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");

        columns.Bound(o => o.Id).Width(50).Title("系统Id").Hidden();
        columns.Bound(o => o.SightId).Width(200).Title("评论景点Id").Hidden();

        columns.Template(@<text><a href = "/sight/detail/@(item.SightId)" rel = "@(item.Id)">@(item.SightName)</a></text>)
        .ClientTemplate("<a href='/sight/detail/<#= SightId #>:;' rel = '<#= Id #>'><#= SightName #></a>")
        .Width(150).Title("评论景点名");

        columns.Bound(o => o.Contents).Width(300).Title("评论内容").HtmlAttributes(new { style = "overflow:hidden;" }).ReadOnly();
        columns.Bound(o => o.UserName).Width(80).Title("用户名").ReadOnly();

        columns.Bound(o => o.IsReply).Width(50).Title("是否回复")
        .Template(@<text>@(item.IsReply ? "是" : "否")</text>).ReadOnly()
        .Hidden(!Html.HasUiPermission(this.ViewContext, "addsightcommreply"));

        columns.Bound(o => o.UserId).Width(200).Title("用户Id").Hidden();
        columns.Bound(o => o.ClientId).Width(200).Title("客户端类型Id").Hidden();
        columns.Bound(o => o.IpAddress).Width(100).Title("添加IP").ReadOnly().Hidden();
        columns.Bound(o => o.AddTime).Width(120).Title("评论时间").Format("{0:yyyy-MM-dd HH:mm:ss}").ReadOnly();
        columns.Bound(o => o.SortNum).Width(100).Title("排序号").Hidden();
        columns.Bound(o => o.State).Width(100).Title("状态").Hidden();

        columns.Template(@<text><a href = "javascript:;" class= 't-button  commReplyCss' rel = "@(item.Id)">回复</a></text>)
        .ClientTemplate("<a href='javascript:;' class = 't-button commReplyCss' rel = '<#= Id #>'>回复</a>")
        .Width(100).Title("回复").Hidden(!Html.HasUiPermission(this.ViewContext, "addsightcommreply"));

        columns.Command(comm =>
        {
            if (Html.HasUiPermission(this.ViewContext, "sightcommreplyindex"))
            {
                comm.Custom("showCommReplyCustom")
                    .Text("查看回复")
                    .Action("sightcommreplyindex", "sight").HtmlAttributes(new { style = "display:none;" });
            }
            if (Html.HasUiPermission(this.ViewContext, "deletesightcomm"))
            {
                comm.Delete().ButtonType(GridButtonType.Text);
            }
        }).Width(100).Title("删除");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding")
        .OnRowDataBound("bindReply").OnComplete("bindComplete"))
    .DataKeys(key => key.Add("Id"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("AjaxSightComment", "sight")
        .Delete("deletesightcomment", "sight", new { targetId = targetId }))
    .Pageable(page => page.PageSize(15))
    .Groupable()
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#SightCommGrid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchSightComm").val();
        var targetId = $('#targetId').val();
        if (searchKeyStr == '评论内容') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { targetId: targetId, searchKey: searchKeyStr });
    }

    function mulDelete() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/sight/muldeletesightcomm', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#SightCommGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }

    function AddCommReply() {
        var commId = $('#sightReplay #commId').val();
        var contents = $('#sightReplay #contents').val();

        if (commId != 0 && contents != '') {
            var data = { CommId: commId, Contents: contents };
            $.post('/sight/addsightcommreply', data, function (res) {
                if (res.success) {
                    $("#SightCommGrid").data("tGrid").rebind();
                    alert('回复成功');
                }
                else {
                    alert(res.message);
                }
            });
        }
        else {
            alert('请填写回复内容');
        }
    }

    function bindReply(e) {
        $('.commReplyCss').bind('click', function () {
            var windowElement = $('#ReplyWindow');
            var targetId = $(this).attr('rel');
            $('#sightReplay #commId').val(targetId);
            windowElement.data('tWindow').center().open();
        })
    }

    function bindComplete(e) {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        bindReply();
        $("input:checkbox[name='selectRow']").attr("checked", false);
        $('#checkAllRecords').click(function checkAll() { $("#SightCommGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: function (request, response) {
                    var url = '/sight/searchsightcomm/';
                    $.getJSON(url, {
                        term: $.trim(request.term),
                        targetId: $('#targetId').val()
                    }, response);
                }
            });
        });

        $("#searchSightComm").focus(function () {
            if (this.value == "评论内容") {
                $("#searchSightComm").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchSightComm").val("评论内容");
            }
        });

        $('#subReply').bind('click', function () {
            AddCommReply();
        });
    });
</script>
